//
//  WDWorkOrderItemViewController.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/16.
//

#import "WDWorkOrderItemViewController.h"
#import "WDWorkOrderItemViewModel.h"
#import "WDWorkOrderItemViewCell.h"
#import "WDWorkOrderItemFirstViewCell.h"

@interface WDWorkOrderItemViewController ()

@property (nonatomic, strong) WDWorkOrderItemViewModel *viewModel;

@end

@implementation WDWorkOrderItemViewController

- (void)dealloc
{
    debugMethod();
}

- (void)initWithVar
{
    [super initWithVar];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(revcFiltrateNoti:) name:kWORK_ORDER_FILTRATE_NOTI object:nil];
}

- (void)initWithViewModel
{
    _viewModel = [WDWorkOrderItemViewModel new];
    _viewModel.reqModel.searchType = @(self.servType);
    [_viewModel setupData];
}

- (void)initWithNavBar
{
    [super initWithNavBar];
    
}

- (void)initWithSubView
{
    [super initWithSubView];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.baseTableView.backgroundColor = [UIColor whiteColor];
    
    [self.baseTableView registerClass:[WDWorkOrderItemViewCell class] forCellReuseIdentifier:[WDWorkOrderItemViewCell className]];
    [self.baseTableView registerClass:[WDWorkOrderItemFirstViewCell class] forCellReuseIdentifier:[WDWorkOrderItemFirstViewCell className]];
    self.baseTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self hiddenFooterView:YES];
    
    [self doRefreshData];
}


/// 收到筛选通知
/// - Parameter noti: noti description
- (void)revcFiltrateNoti:(NSNotification *)noti {
    WDStationTaskSearchReqModel *model = [noti object];
    if (model.searchType == self.viewModel.reqModel.searchType) {
        self.viewModel.reqModel.startTime = model.startTime;
        self.viewModel.reqModel.endTime = model.endTime;
        self.viewModel.reqModel.servFlow = model.servFlow;
        self.viewModel.reqModel.engineerId = model.engineerId;
        
        [self doRefreshData];
    }
}

- (void)doRefreshData
{
    [super doRefreshData];
    self.viewModel.reqModel.current = 1;
    [self fromServerRequestData];
}

- (void)doMoreRefreshData {
    [super doMoreRefreshData];
    
    [self fromServerRequestData];
}

- (void)fromServerRequestData {
    
    @weakify(self);
    [self.viewModel requestWithTaskSearch:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [self.baseTableView reloadData];
        }
        [self endAllRefreshing];
        
        if (self.viewModel.dataList.count < 10) {
            [self hiddenFooterView:YES];
        } else {
            [self hiddenFooterView:NO];
        }
    }];
     
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.dataList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return kWDWorkOrderItemFirstViewCellHeight;
    }
    return kWDWorkOrderItemFirstViewCellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WDArrivalServiceItemModel *model = [self.viewModel.dataList objectOrNilAtIndex:indexPath.row];
    if (indexPath.row == 0) {
        WDWorkOrderItemFirstViewCell *cell = [WDWorkOrderItemFirstViewCell cellForTableView:tableView indexPath:indexPath];
        [cell updateCellData:model];
        return cell;
    }
    WDWorkOrderItemViewCell *cell = [WDWorkOrderItemViewCell cellForTableView:tableView indexPath:indexPath];
    [cell updateCellData:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (![[EDSAppDelegateTool sharedInstance] hasPermission:@(10030101) parentCode:@(100301)]) {
        [SVProgressHUD showInfoWithStatus:@"您没有查看详情的权限！"];
        return;
    }
    
    
    WDArrivalServiceItemModel *model = [self.viewModel.dataList objectOrNilAtIndex:indexPath.row];
    
    @weakify(self);
    void(^block)(id data) = ^(id data) {
        @strongify(self);
        [self doRefreshData];
    };
    
    NSDictionary *params = @{
        @"serviceModel": model,
        @"block":block
    };
//    [SVProgressHUD showInfoWithStatus:@"详情页面暂未开发！"];
    UIViewController *vc = nil;
    // 服务流程 0.待分派 1.待接车 2.检查中 3.维修中 4.待报单 5.已完成 6.已取消 7.已关闭
    if (model.taskType == 1) { // 外出救援
        if (model.servFlow == 0) { // 待分派
            vc = [[TYZRouter sharedInstance] getGoutWaitAssignVC:params];
        } else if (model.servFlow == 1) { // 待接车
            vc = [[TYZRouter sharedInstance] getGrabWaitVehicleVC:params];
        } else if (model.servFlow == 2) { // 检查中
            vc = [[TYZRouter sharedInstance] getGrabReviewVehicleVC: params];
        } else if (model.servFlow == 3) { // 维修中
            vc = [[TYZRouter sharedInstance] getGrabMaintenanceVehicleVC:params];
        } else if (model.servFlow == 4) { // 待报单
            vc = [[TYZRouter sharedInstance] getGrabPendingVehicleVC:params];
        } else {
            vc = [[TYZRouter sharedInstance] getGoutPendingVehicleDetailVC:params];
        }
    } else { // 站内维修
        if (model.servFlow == 1) { // 待接车
            vc = [[TYZRouter sharedInstance] getServiceOrderWaitVehicleVC:params];
        } else if (model.servFlow == 2) { // 检查中
            vc = [[TYZRouter sharedInstance] getServiceOrderReviewVehicleVC: params];
        } else if (model.servFlow == 3) { // 维修中
            vc = [[TYZRouter sharedInstance] getServiceOrderMaintenanceVehicleVC:params];
        } else if (model.servFlow == 4) { // 待报单
            vc = [[TYZRouter sharedInstance] getServiceOrderPendingVehicleVC:params];
        } else {
            vc = [[TYZRouter sharedInstance] getServiceOrderPendingVehDetailVC:params];
        }
    }
    
    /*
    if (model.taskType == 1) { // 外出救援
        if (model.servFlow == 0) { //待分派
            vc = [[TYZRouter sharedInstance] getGoutWaitAssignDetailVC:params];
        } else if (model.servFlow == 1) { // 待接车
            vc = [[TYZRouter sharedInstance] getGoutWaitVehicleDetailVC:params];
        } else if (model.servFlow == 2) { // 检查中
            vc = [[TYZRouter sharedInstance] getGoutReviewVehicleDetailVC:params];
        } else if (model.servFlow == 3) { // 维修中
            vc = [[TYZRouter sharedInstance] getGoutMaintenanceVehicleDetailVC:params];
        } else if (model.servFlow == 4) { // 待报单
            vc = [[TYZRouter sharedInstance] getGoutPendingVehicleDetailVC:params];
        } else {
            vc = [[TYZRouter sharedInstance] getGoutPendingVehicleDetailVC:params];
        }
    } else { // 站内维修
        if (model.servFlow == 1) { // 待接车
            vc = [[TYZRouter sharedInstance] getServiceOrderWaitVehDetailVC:params];
        } else if (model.servFlow == 2) { // 检查中
            vc = [[TYZRouter sharedInstance] getServiceOrderReviewVehDetailVC:params];
        } else if (model.servFlow == 3) { // 维修中
            vc = [[TYZRouter sharedInstance] getServiceOrderMainVehDetailVC:params];
        } else if (model.servFlow == 4) { // 待报单
            vc = [[TYZRouter sharedInstance] getGoutPendingVehicleDetailVC:params];
        } else {
            vc = [[TYZRouter sharedInstance] getServiceOrderPendingVehDetailVC:params];
        }
    }*/
    
    if (vc) {
        [TYZJumpVCManager pushBaseVC:self pushVC:vc];
    }
}


@end
